package repositories;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;

import specifications.AircraftSpecification;
import dbHelper.DBHelper;
import entities.Aircraft;



public class AircraftRepository implements Repository<Aircraft> {
	
	
	public ArrayList<Aircraft> AircraftList;
	
	
	public AircraftRepository()
	{
		this.AircraftList = null;
	}

	@Override
	public void Add(Aircraft Aircraft) {
		try {
			DBHelper.addObject(Aircraft);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Delete(Aircraft Aircraft) {
		try {
			DBHelper.deleteObject(Aircraft);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Update(Aircraft Aircraft) {
		try {
			DBHelper.updateObject(Aircraft);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void FindAll() {
		Aircraft A = new Aircraft();
		try {
			this.AircraftList = (ArrayList<Aircraft>) DBHelper.findObjects(A);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | InstantiationException
				| NoSuchMethodException | SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ArrayList<Aircraft> findBySpecification (AircraftSpecification spec) {
		if(this.AircraftList == null)
			this.FindAll();
		ArrayList<Aircraft> res = new ArrayList<Aircraft>();
		for (Aircraft el : AircraftList)
		{
			if (spec.isSatisfiedBy(el))
			{
				res.add(el);
			}
		}
		return res;
	}
 
}
